<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <common-head></common-head>
  </div>
  <script src="./vue.js"></script>
  <script src="./axios.js"></script>
  <script>
  //  https://api.it120.cc/conner/shop/goods/category/all 请求商品列表
    Vue.component('CommonHead', {
      template: `
        <div>
          <h2>我是公共的头部</h2>  
          <ul>
            <li v-for="cate in cates" :key="cate.id">
              <img :src="cate.icon"/>  
              <h3>{{cate.name}}</h3>
            </li>  
          </ul>
        </div>
      `,
      created(){
        /* 
          页面初始化时 调用 初始化 需要用到的请求函数
        */
        // 3 created中调用这个方法  
        this.fetchCates();
      },
      data(){
        return {
          // 1 在data中定义初始化 接收请求成功的数据
          cates: []
        }
      },
      methods: {
        // 2在methods中定义方法 发送请求
        fetchCates(){
          axios.get('https://api.it120.cc/conner/shop/goods/category/all').then(res => {
            if(res.data.code === 0) {
              this.cates = res.data.data
            }
          })
        }
      }
    })
    const vm = new Vue({
      el: '#app'
    })
    console.log(vm);
  </script>
</body>
</html>